﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Abp.Threading;
using DHJ.Messages.Cost.RealTime;
using NServiceBus;
using Newtonsoft.Json;
using DHJ.Messages.Production.Processes;

namespace MessageTool
{
    class Program
    {
        static async Task Main(string[] args)
        {


            var instance = await CreateEndpointInstance() ;

            //var message = new ProcessToolingUsed()
            //{
            //    Workshop = "53",
            //    BatchId = "ZC2019PC53I000003550",
            //    Quantity = 1,
            //    Sequence = 18,
            //    Name = "工序2：制展开",
            //    ToolingNumber = "塞规",
            //    DurationOfUse = 3.5m
            //    //EquipmentNumber = "PR6C150×3100",
            //    //DurationOfUse = 1.5m
            //    //ManHour = 6
            //};

            var message = new ProcessToolingUsed()
            {
                Workshop = "53",
                BatchId = "ZC2019PC53I000003442",
                Quantity = 1,
                Sequence = 37,
                Name = "工序3:预拉弯成形",
                ToolingNumber = "4°角度样板",
                DurationOfUse = 3.5m
            };



            await instance.Publish(message).ConfigureAwait(false);
            Console.WriteLine("消息：" + JsonConvert.SerializeObject(message));

            //Console.WriteLine("请选择生成的消息类型：");
            //Console.WriteLine("");
            //Console.WriteLine("0. 特定成本");
            //Console.WriteLine("1. 成本");

            //var t = Console.ReadLine();

            //var n = "Y";

            //var strs = new[] {"n", "N"};

            //while (!strs.Any(e => n != null && n.Contains(e)))
            //{

            //    if (Convert.ToInt32(t) == 1)
            //    {
            //        Console.WriteLine("");
            //        Console.WriteLine("请输入消息数量(输入 n|N 退出)：");
            //        Console.WriteLine("");

            //        n = Console.ReadLine();

            //        Console.WriteLine("开始发送消息...");

            //        await SendCostMessages(instance, Convert.ToInt32(n));
            //    }
            //    else if(Convert.ToInt32(t) == 0)
            //    {
            //        Console.WriteLine("");
            //        Console.WriteLine("请输入车间(51, 53, 55, 56, 57, 58, 4001, 4002)：");
            //        var workshop = Console.ReadLine();

            //        Console.WriteLine("");
            //        Console.WriteLine("请输入类型(X.X)：");
            //        var type = Console.ReadLine();

            //        Console.WriteLine("");
            //        Console.WriteLine("请输入条码(TiaoMaHaoX)：");
            //        var batchId = Console.ReadLine();


            //        var message = new CostEstimated()
            //        {
            //            Amount = 100,
            //            BatchId = batchId,
            //            Quantity = 1,
            //            Time = DateTime.Now,
            //            Type = type,
            //            Workshop = workshop,
            //            ProcessSequence = 1,
            //            Formula = "XXX*XXX+XXX"
            //        };

            //        message.ProcessName = "GongXu_" + message.ProcessSequence;

            //        await instance.Publish(message).ConfigureAwait(false);

            //        Console.WriteLine("消息：" + JsonConvert.SerializeObject(message));


            //    }
            //    else
            //    {
            //        break;
            //    }

            //}

            //if (!strs.Any(e => n != null && n.Contains(e)))
            //{
            //    Console.ReadKey();
            //}

            await instance.Stop();
        }

        private static async Task<IEndpointInstance> CreateEndpointInstance()
        {

            var endpointConfiguration = new EndpointConfiguration("Client");

            var transport = endpointConfiguration.UseTransport<RabbitMQTransport>();

            transport.UseConventionalRoutingTopology();

            transport.ConnectionString("host=10.20.253.2;username=barcode;password=tjhj123456,");
            endpointConfiguration.SendFailedMessagesTo("Client_error");
            endpointConfiguration.EnableInstallers();
            
            return await Endpoint.Start(endpointConfiguration)
                .ConfigureAwait(false);
        }


        private static async Task SendCostMessages(IEndpointInstance instance, int n)
        {
            if (n < 1)
            {
                return;
            }

            var ran = new Random();

            var workshops = new[] {51, 53, 55, 56, 57, 58, 4001, 4002};

            Console.WriteLine("");

            for (int i = 0; i < n; i++)
            {
                var message = new CostEstimated()
                {
                    Amount = Convert.ToDecimal(ran.NextDouble() * 900 + 100),
                    BatchId = "TiaoMaHao" + ran.Next(1, 21),
                    Quantity = ran.Next(1, 6),
                    Time = DateTime.Now,
                    Type = ran.Next(1, 10).ToString(),
                    Workshop = workshops[ran.Next(8)].ToString(),
                    ProcessSequence = ran.Next(1, 6),
                    Formula = "XXX*XXX+XXX"
                };
                message.ProcessName = "GongXu_" + message.ProcessSequence;
                await instance.Publish(message).ConfigureAwait(false);

                Console.WriteLine("消息：" + JsonConvert.SerializeObject(message));
            }

            Console.WriteLine("");
            Console.WriteLine("消息发送成功...");
        }



    }
}
